/*
 * @Author: zhengzhuang
 * @Date: 2021-01-23 10:01:36
 * @LastEditors: zhengzhuang
 * @LastEditTime: 2021-01-23 15:29:11
 * @Description: app启动广告图片表
 * @FilePath: /C20Driver/lib/database/advert_picture_base.dart
 */
import 'dart:typed_data';

import 'package:C20Driver/database/base_db_provider.dart';
import 'package:sqflite/sqflite.dart';

class AdvertPictureBase extends BaseDbProvider {
  ///表名
  final String name = 'AdvertPictureBase';

  final String _id = "id";
  final String advertPictureImg = "advert_picture_img";

  AdvertPictureBase();

  @override
  tableName() {
    return name;
  }

  @override
  createTableString() {
    return '''
        create table $name (
        $_id integer primary key,$advertPictureImg text not null)
      ''';
  }

  ///查询数据库
  Future _getPersonProvider(Database db) async {
    List<Map<String, dynamic>> maps = await db.rawQuery("select * from $name");
    return maps;
  }

  ///插入到数据库
  // Future insert(Uint8List model) async {
  Future insert(String model) async {
    Database db = await getDataBase();
    var userProvider = await _getPersonProvider(db);
    if (userProvider != null) {
      ///删除数据
      await db.delete(name, where: "$_id = ?", whereArgs: [1, model]);
    }
    print("插入到数据库");
    print(model);
    // return await db.insert('$name', {
    //   "advertPictureImg": model
    // }, conflictAlgorithm: ConflictAlgorithm.replace, );
    return await db.rawInsert(
        "insert into $name ($_id, $advertPictureImg) values (?,?)", [1, model]);
    // INSERT INTO first_table_name [(column1, column2, ... columnN)]  SELECT column1, column2, ...columnN  FROM second_table_name [WHERE condition];
  }

  // Future commitList(List<GetTabConfigModel> model) async {
  //   Database db = await getDataBase();
  //   await db.delete(name, where: "1");
  //   Batch batch = db.batch();

  //   model.forEach((GetTabConfigModel element) async {
  //     batch.insert(name, element.toJson());
  //   });
  //   return await batch.commit();
  //   // var userProvider = await _getPersonProvider(db);
  //   // if (userProvider != null) {
  //   //   ///删除数据
  //   //   await db.delete(name, where: "$_id = ?", whereArgs: [model.id]);
  //   // }
  //   // return await db.rawInsert("insert into $name ($_id,$title,$localIcon,$fullImgUrl) values (?,?,?,?)", [model.id, model.title, model.localIcon, model.fullImgUrl]);
  //   // INSERT INTO first_table_name [(column1, column2, ... columnN)]  SELECT column1, column2, ...columnN  FROM second_table_name [WHERE condition];
  // }

  // ///更新数据库
  Future<void> update(Uint8List model) async {
    Database database = await getDataBase();
    await database.rawUpdate(
        "update $name set $advertPictureImg = ? where $_id= ?", [model]);
  }

  ///获取事件数据
  Future<List<Map<String, dynamic>>> getPersonInfo() async {
    Database db = await getDataBase();
    List<Map<String, dynamic>> maps = await _getPersonProvider(db);
    if (maps.length > 0) {
      return maps;
      // return GetTabConfigModel.fromJson(maps[0]);
    }
    return null;
  }
}
